# 定义一个过滤器
import functools

from flask import session, current_app, g

from info.models import User


def func_index_convert(index):
    index_dict = {1:"first",2:"second",3:"third"}

    return index_dict.get(index,'')


def user_login_data(f):
    @functools.wraps(f) # 装饰后endpoint 还是f不是wrapper
    def wrapper(*args,**kwargs):
        # 判断是否用户是否登陆
        # 取不出来是none
        user_id = session.get("user_id")

        user = None  # type: User
        #根据user_id取用户
        if user_id:
            try:
                user = User.query.get(user_id)
            except BaseException as e:
                current_app.logger.error(e)
        # 用上下文 g变量 保存user
        g.user = user
        return f(*args,**kwargs)

    return wrapper